﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlugIn
{
    public static class FormExtension
    {
        /// <summary>
            /// 动态给公共属性赋值
            /// </summary>
            /// <param name="form"></param>
            /// <param name="propertyName"></param>
            /// <param name="propertyValue"></param>
        public static void SetPublicPropertyValue(this Form form, string propertyName, object propertyValue)
        {
            var formType = form.GetType();
            var property = formType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
            if (property != null)
            {
                property.SetValue(form, propertyValue, null);
            }
            else
            {
                throw new Exception(string.Format("没有找到名称为：{0}的公共属性成员！", propertyName));
            }
        }

        /// <summary>
            /// 动态获取公共属性的值
            /// </summary>
            /// <typeparam name="TResult"></typeparam>
            /// <param name="form"></param>
            /// <param name="propertyName"></param>
            /// <param name="defaultPropertyValue"></param>
            /// <returns></returns>
        public static TResult GetPublicPropertyValue<TResult>(this Form form, string propertyName, TResult defaultPropertyValue)
        {
            var formType = form.GetType();
            var property = formType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
            var proValue = property.GetValue(form, null);
            if (property != null)
            {
                try
                {
                    return (TResult)Convert.ChangeType(proValue, typeof(TResult));
                }
                catch
                {
                    return defaultPropertyValue;
                }
            }
            else
            {
                throw new Exception(string.Format("没有找到名称为：{0}的公共属性成员！", propertyName));
            }
        }

        /// <summary>
            /// 动态执行公共方法（含公共静态方法）
            /// </summary>
            /// <param name="form"></param>
            /// <param name="methodName"></param>
            /// <param name="args"></param>
            /// <returns></returns>
        public static object ExecutePublicMethod(this Form form, string methodName, params object[] args)
        {
            var formType = form.GetType();
            var method = formType.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase);
            if (method != null)
            {
                return method.Invoke(form, args);
            }
            else
            {
                throw new Exception(string.Format("没有找到名称为：{0}且形数个数有：{1}个的公共方法成员！", methodName, args == null ? 0 : args.Count()));
            }
        }
    }
}
